<template>
  <div class="user-list-container">
    <div class="local-name">
      用户列表
      <el-button
        type="primary"
        :loading="btnLoading"
        @click="addUserRole"
      >保存</el-button>
    </div>
    <div class="user-list-search">
      <el-form inline>
        <el-form-item>
          <el-input
            size="small"
            placeholder="账户/用户名"
            clearable
            v-model="searchForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            placeholder="邮箱"
            clearable
            v-model="searchForm.email"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            placeholder="手机号"
            clearable
            v-model="searchForm.mobile"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="search"
            size="small"
            :loading="searchForm.btnLoading"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="data-table">
      <el-table
        size="small"
        :data="tableData"
        stripe
        border
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          align="center"
          label="账号"
        ></el-table-column>
        <el-table-column
          prop="displayName"
          align="center"
          label="用户名"
        ></el-table-column>
        <el-table-column
          prop="email"
          align="center"
          label="邮箱"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          align="center"
          label="手机号"
        ></el-table-column>
        <el-table-column
          prop="createdAt"
          sortable="custom"
          align="center"
          label="创建时间"
        ></el-table-column>
      </el-table>
    </div>
    <div
      align="right"
      class="paging-footer"
    >
      <el-pagination
        background
        @size-change="pageSizeChangeHandler"
        @current-change="currentPageChangeHandler"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :page-sizes="pageSizes"
        layout="total,sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { Message, MessageBox } from 'element-ui';
import { getUserListApi, updateUserRoleBatchApi } from '../../../api/user';
import { MomentFormat, SortType } from '../../../constants/common';
import moment from 'moment';
import { parseError } from '../../../utils';

export default {
  name: 'SelectUser',
  data() {
    return {
      tableData: [],
      totalCount: 0,
      currentPage: 1,
      pageSize: 10,
      pageSizes: [5, 10, 20, 50, 100],
      sort: {
        sortType: SortType.DESC,
        orderBy: 'createdAt'
      },
      selectedUserList: [],
      searchForm: {
        name: '',
        email: '',
        mobile: null,
        btnLoading: false
      },
      btnLoading: false,
      organizationId: null
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.searchForm.btnLoading = true;
      getUserListApi(
        this.currentPage - 1,
        this.pageSize,
        this.sort.sortType,
        this.sort.orderBy,
        this.organizationId,
        this.searchForm.name,
        this.searchForm.email,
        this.searchForm.mobile,
        this.$route.query.uuid,
        false
      )
        .then(({ data, total_count }) => {
          this.totalCount = total_count;
          this.tableData = data.map(selectedRoleList => {
            return {
              uuid: selectedRoleList.uuid,
              username: selectedRoleList.username,
              displayName: selectedRoleList.display_name,
              email: selectedRoleList.email,
              mobile: selectedRoleList.mobile,
              createdAt: moment(selectedRoleList.created_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(error))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    handleSortChange(res) {
      this.sort.sortType = res.order === 'descending' ? SortType.DESC : SortType.ASC;
      this.search();
    },
    handleSelectionChange(res) {
      this.selectedUserList = res;
    },
    addUserRole() {
      MessageBox.confirm('是否确认更新', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.btnLoading = true;
          updateUserRoleBatchApi(
            this.$route.query.uuid,
            this.selectedUserList.map(item => item.uuid)
          )
            .then(res => {
              this.$router.back();
            })
            .catch(error => Message.error(parseError(error)))
            .finally(() => (this.btnLoading = false));
        })
        .catch(() => {});
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>

<style lang="scss" scoped>
.user-list-container {
  .user-list-search {
    background-color: #fff;
    padding: 10px;
    margin-bottom: 10px;
    .el-form-item {
      margin-bottom: 0px;
    }
    .el-button--primary {
      background-color: #2c83b1 !important;
      border-color: #2c83b1 !important;
    }
  }
  .data-table {
    padding: 10px;
    background-color: #fff;
  }
  padding: 10px;
  .local-name {
    font-size: 24px;
    font-weight: 800;
    padding: 10px 10px 20px 10px;
    .el-button--primary {
      float: right;
      background-color: #2c83b1 !important;
      border-color: #2c83b1 !important;
    }
  }
  .paging-footer {
    background-color: #fff;
    padding: 10px;
  }
}
</style>
